package com.cayden.commons.httpclient;

import com.cayden.commons.constants.CaydenHttpConstants;
import org.apache.http.client.methods.CloseableHttpResponse;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;

/**
 * @author: cayden
 * @Since since in 1997
 * Date: 2022/2/21 01:17
 */
public class CaydenHttpResponse implements Serializable {

    private static final long serialVersionUID = 1907789469385061664L;

    protected CloseableHttpResponse httpResponse;

    public CaydenHttpResponse(CloseableHttpResponse response){
        this.httpResponse = response;
    }

    public CloseableHttpResponse httpResponse(){
        return httpResponse;
    }

    public String responseAsString(){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            httpResponse.getEntity().writeTo(baos);
            return new String(baos.toByteArray(), CaydenHttpConstants.ENCODING);
        }catch (IOException e){
            throw new IllegalStateException(e);
        }
    }

    public boolean is200(){
        return httpResponse.getStatusLine().getStatusCode() == 200;
    }
}
